package kalambury.srv;

import kalambury.db.ScoreEntity;
import kalambury.db.UserEntity;

import javax.persistence.*;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ufoq
 * Date: 06.11.12
 * Time: 22:17
 * To change this template use File | Settings | File Templates.
 */

/**
 * klasa pomocnicza do obslugi bazy danych
 */
public final class DbHelper {

    /**
     * pobiera fabrykę EM
     * @return fabryka FM
     */
    public static EntityManagerFactory createEntityManagerFactory() {
        return Persistence.createEntityManagerFactory("NewPersistenceUnit");
    }

    /**
     * tworzy EM za pomocą fabryki
     * @return EM
     */
    public static EntityManager createEntityManager() {
        return createEntityManagerFactory().createEntityManager();
    }

    /**
     * zwraca użytkowników z bazy
     * @return lista użytkowników
     */
    public static List<UserEntity> getUsers() {
        EntityManager em = createEntityManager();

        String queryString = "SELECT * FROM user";
        Query query = em.createNativeQuery(queryString, UserEntity.class);

        List<UserEntity> users = query.getResultList();

        em.close();
        return users;
    }

//    public static List<ScoreEntity> getScores() {
//        EntityManager em = createEntityManager();
//
//        String queryString = "SELECT * FROM score";
//        Query query = em.createNativeQuery(queryString, ScoreEntity.class);
//
//        List<ScoreEntity> scores = query.getResultList();
//
//        //em.createNativeQuery()
//
//        em.close();
//        return scores;
//    }
}
